//
//  UIChooseTableView.m
//  ERP
//
//  Created by apple on 14-11-16.
//  Copyright (c) 2014年 com.sotao. All rights reserved.
//

#import "UIChooseTableView.h"
#define ScreenWidth [UIScreen mainScreen].bounds.size.width

#define ScreenHeight [UIScreen mainScreen].bounds.size.height
@implementation UIChooseTableView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {

        // Initialization code
        self.mainTabel = [[UITableView alloc]initWithFrame:CGRectMake(0, 45, ScreenWidth, 215)];

        self.mainTabel.backgroundColor = [UIColor whiteColor];
        self.mainTabel.delegate = self;
        self.mainTabel.dataSource = self;
        self. mainTabel.showsVerticalScrollIndicator = NO;
        self.mainTabel.tableFooterView = [[UIView alloc] init]; //xhw2018-3-12加

        self.view_title = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 45)];
        self.view_title.backgroundColor = [UIColor blackColor];
        _view_title.layer.opacity = 0.7;
        UIButton *btn_title =[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 45)];
        [btn_title setTitle:@"取消" forState:UIControlStateNormal];
        btn_title.titleLabel.font = [UIFont boldSystemFontOfSize:18];
        [btn_title setTitleEdgeInsets:UIEdgeInsetsMake(0, -30, 0, 0)];
 
        [btn_title setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [btn_title addTarget:self action:@selector(hideTabelView) forControlEvents:UIControlEventTouchUpInside];
        
        self.lanel_title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 45)];
 
        self.lanel_title.font = [UIFont boldSystemFontOfSize:16];
        self.lanel_title.textColor = [UIColor whiteColor];
        self.lanel_title.textAlignment =NSTextAlignmentCenter;
        
        [self.view_title addSubview:btn_title];
        [self.view_title addSubview:self.lanel_title];
        
        [self addSubview:self.mainTabel];
        [self addSubview:self.view_title];
     
        self.datePickerViewBg = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        self.datePickerViewBg.backgroundColor = [UIColor grayColor];
        self.datePickerViewBg.layer.opacity = 0.4;
        
        [[[[UIApplication sharedApplication] windows] firstObject] addSubview:self.datePickerViewBg];
    }
    return self;
}


-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.mainData.count;
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellname = @"Choosecell";
    UILabel *label_tabel = nil;

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellname];
    
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellname];
   
    label_tabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 40)];
    label_tabel.textAlignment = NSTextAlignmentCenter;
    label_tabel.text = [[self.mainData objectAtIndex:indexPath.row] objectForKey:@"name"];
    [cell.contentView addSubview:label_tabel];
 
    
    return cell;
    
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self down];
    if ([self.delegate respondsToSelector:@selector(didSelectDate: indexPath: string:)]) {
        [self.delegate didSelectDate:tableView indexPath:indexPath string:[self.mainData objectAtIndex:indexPath.row]];
    }
    
}

-(void)up{
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.6];//动画时间长度，单位秒，浮点数
    [self exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
    self.frame = CGRectMake(0, ScreenHeight-260, ScreenWidth, 240);
 
    [UIView setAnimationDelegate:self];
    // 动画完毕后调用animationFinished
    [UIView setAnimationDidStopSelector:@selector(animationFinished)];
    [UIView commitAnimations];
}

-(void)down{
    CGContextRef context = UIGraphicsGetCurrentContext();
    [UIView beginAnimations:nil context:context];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.6];//动画时间长度，单位秒，浮点数
    self.frame = CGRectMake(0,ScreenHeight, ScreenWidth, 240);
    [UIView setAnimationDelegate:self];
    // 动画完毕后调用animationFinished
    [UIView setAnimationDidStopSelector:@selector(animationFinished)];
    [UIView commitAnimations];
    [self.datePickerViewBg removeFromSuperview];
    [self removeFromSuperview];
    
}
-(void)animationFinished{
    NSLog(@"动画结束!");
}

-(void)hideTabelView{
    [self down];
    
    [self removeFromSuperview];
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect
 {
 // Drawing code
 }
 */

@end
